package leetcode.L500;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Ex35 {
      public static class ListNode {
      int val;
      ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  }

        public ListNode reverseKGroup(ListNode head, int k) {
            ListNode cur = head;
            int num = 0;
            while(cur!=null){
                cur = cur.next;
                num++;
            }
            if(num%k!=0||k==1){
                return head;
            }
            cur = head;
            ListNode res = new ListNode(-1,head);
            ListNode pre = res;
            int i = 0;
            while(cur!=null){
                if(i%k==0){
                    ListNode out = help(cur,k); //cur 是出节点out是入节点
                    pre.next = out;
                    pre = cur;
                }
                cur = cur.next;
                i++;
            }
            cur.next = null;
            return res.next;
        }

        public ListNode help(ListNode head,int k){
            ListNode cur = head.next;
            ListNode pre = head;
            int index = 3;
            if(k==2){
                cur.next = pre;
                return cur;
            }else{
                ListNode ne = cur.next;
                for(int i=3;i<=k;i++){
                    cur.next = pre;
                    pre = cur;
                    cur = ne;
                    ne = ne.next;
                }
                ne.next = cur;
                return ne;
            }
        }

    public static ListNode stringToListNode(String input) {
        // Generate array from the input
        int[] nodeValues = stringToIntegerArray(input);

        // Now convert that list into linked list
        ListNode dummyRoot = new ListNode(0);
        ListNode ptr = dummyRoot;
        for(int item : nodeValues) {
            ptr.next = new ListNode(item);
            ptr = ptr.next;
        }
        return dummyRoot.next;
    }
    public static int[] stringToIntegerArray(String input) {
        input = input.trim();
        input = input.substring(1, input.length() - 1);
        if (input.length() == 0) {
            return new int[0];
        }

        String[] parts = input.split(",");
        int[] output = new int[parts.length];
        for(int index = 0; index < parts.length; index++) {
            String part = parts[index].trim();
            output[index] = Integer.parseInt(part);
        }
        return output;
    }

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String line;
        while ((line = in.readLine()) != null) {
            ListNode head = stringToListNode(line);
            line = in.readLine();
            int k = Integer.parseInt(line);

            ListNode ret = new Ex35().reverseKGroup(head, k);
            
        }
    }


}
